/**
 * 库存详情storeDetailDetailList.js
 * 版本号： v1.0.0
 */
$(function () {
    
    /**
     * 库存详情
     */
    var lastChecked = 0;
    var coId = $('#coId').val();
    $("#storeDetail_dataList").datagrid({
        fit: true,
        fitColumns: true,
        method: "POST",
        rownumbers: true,
        singleSelect: true,
        border: false,
        striped: true,
        url: CONTEXT_PATH + '/store/storeDetailList.do?coId='+coId,
        idField: 'stId',
        pagination: true,
        pageSize: 10,
        pageList: [10, 50, 100],
        frozenColumns: [[
            {field: 'stId', checkbox: true}
        ]],
        columns: [
            [
                {field: 'material', title: '材料名称', width: 160, fixed: true,
                    formatter:function(value){
                        if(value){
                            return value.materialName;
                        }
                    }},
                {field: 'standardType_', title: '材料规格', width: 200, fixed: true,
                    formatter:function(value,row){
                        if(row.material){
                            return row.material.standardType;
                        }
                    }},
                {field: 'storeNum', title: '库存数量', width: 80, fixed: true},
                {field: 'meteringUnit_', title: '计量单位', width: 80, fixed: true,
                    formatter:function(value,row){
                        if(row.material){
                            return row.material.meteringUnit;
                        }
                    }},
                {
                    field: 'supplier', title: '材料供应商', width: 120,fixed: true,
                    formatter: function (value) {
                        if (value) {
                            return value.supName;
                        }
                    }
                },
                {
                    field: 'storageInRecord', title: '入库人员', width: 100,
                    formatter: function (value) {
                        if (value) {
                            return value.pushPerson;
                        }
                    }
                }
            ]
        ],
        onCheck: function (rowIndex, rowData) {
            var row = $("#storeDetail_dataList").datagrid("getChecked");
            if (row && rowIndex == lastChecked) {
                $('#storeDetail_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            } else {
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess: function (data) {
            if (data.rows.length > 0) {
                lastChecked = null;
                $("#storeDetail_dataList").datagrid("selectRow", 0);
            }
        }
    });


    //定义可操作按钮
    $("#storeDetail_menu").dialog({
        title: '',
        width: 'auto',
        height: 35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });


});


/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for (var j = 0; j < btnOptions.length; j++) {
        if (false != btnOptions[j].value) {
            falg = true;
            break;
        }
    }
    if (!falg) {
        //不存在按钮
        toolbar.push({});
    } else {
        //存在按钮
        for (var i = 0; i < btnOptions.length; i++) {
            if (false != btnOptions[i].value) {
                //查询按钮
                if ("searchBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id: btnOptions[i].value.meId,
                        text: btnOptions[i].value.name,
                        iconCls: btnOptions[i].value.iconCls,
                        handler: function () {
                            storeDetail_loadSearch();
                        }
                    });
                }
                //出入库详情
                if ("inAndOutBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id: btnOptions[i].value.meId,
                        text: btnOptions[i].value.name,
                        iconCls: btnOptions[i].value.iconCls,
                        handler: function () {
                            inAndOut_detail($(this).linkbutton("options").iconCls);
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 *  加载查询
 */
function storeDetail_loadSearch() {
    //刷新dg页码
    refreshPageNumber("storeDetail_dataList");
    //查询数据
    $("#storeDetail_dataList").datagrid("reload", {
        materialName: $("#materialName").textbox("getValue"),
        standardType: $("#standardType").textbox("getValue")
    });
}

/**
 * 出入库详情
 */
function inAndOut_detail(iconCls) {
    var row = $("#storeDetail_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({title: '操作提示', msg: "请选择库存信息", timeout: 3000, showType: "slide"});
        return;
    }
    var row = $("#storeDetail_dataList").datagrid("getSelected");
    //IFRAM打开窗口
    layoutSub(crkjl.id, crkjl.url + row.stId, iconCls, crkjl.closable, crkjl.title);
}